package com.csl.jsonpreference

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.csl.jsonpreference.databinding.ActivityMainBinding
import com.csl.jsonpreference.test.TestBean
import java.util.zip.Inflater


class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding

    var activityMode = false

    companion object {
        val REQUEST_CODE_JSON = 987
    }

    var testJson = """
{
    "a1": {
        "a1": 1,
        "a2": false,
        "a3": "asdfasdf"
    },
    "a2": 1,
    "a3": 8.9,
    "a4": [
    {
        "a1": 1,
        "a2": false,
        "a3": "asdfasdf"
    },
    123,
    "aksfdjekjr"
    ]
}
                """.trimIndent()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)


        binding.btnJsonDialog.setOnClickListener {
            if (activityMode) {
                activityMode = false
                binding.btnJsonDialog.text = "dialog模式"
            } else {
                activityMode = true
                binding.btnJsonDialog.text = "activity模式"
            }
        }


        binding.btnClassmode.setOnClickListener {
            if (activityMode) {
                JsonPreferenceUtil.startConfigForResult(
                    this,
                    REQUEST_CODE_JSON,
                    binding.tvContent.text.toString(),
                    TestBean::class.java
                )
            } else {
                JsonPreferenceUtil.startConfigForDialog(
                    this,
                    binding.tvContent.text.toString(),
                    TestBean::class.java
                ) {
                    if (it != null) {
                        binding.tvContent.text = it
                    }
                }
            }
        }
        binding.btnJsonstrmod.setOnClickListener {
            if (activityMode) {
                JsonPreferenceUtil.startConfigForResult(
                    this,
                    REQUEST_CODE_JSON,
                    testJson,
                    null,
                    "自定义json"
                )
            } else {
                JsonPreferenceUtil.startConfigForDialog(
                    this,
                    testJson,
                    null,
                    "自定义json"
                ) {
                    if (it != null) {
                        binding.tvContent.text = it
                        testJson = it
                    }
                }
            }
        }

    }


    override fun onActivityReenter(resultCode: Int, data: Intent?) {
        super.onActivityReenter(resultCode, data)

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        var str = JsonPreferenceUtil.getResultJson(resultCode, data)
        if (str != null) {
            binding.tvContent.text = str
        }
    }

}

